package com.eoner.baselibrary.utils;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 作者：FuZhi
 * 创建时间：2018/8/8
 * 功能说明：
 */
public class ToolbarUtil {

    private Toolbar toolbar;
    private AppCompatActivity activity;

    private ToolbarUtil(Toolbar toolbar, AppCompatActivity activity){
        this.toolbar = toolbar;
        this.activity = activity;
    }

    public static ToolbarUtil getInstance(Toolbar toolbar, AppCompatActivity activity){
        return new ToolbarUtil(toolbar, activity);
    }

    public ToolbarUtil setBarColor(int color){
        toolbar.setBackgroundColor(color);
        return this;
    }

    public ToolbarUtil setBarDrawable(int resId){
        toolbar.setBackgroundResource(resId);
        return this;
    }

    public ToolbarUtil setBackIcon(Integer back_img, View.OnClickListener listener) {
        ImageView back_iv = (ImageView) toolbar.getChildAt(0);
        back_iv.setImageResource(back_img);
        if(listener != null){
            back_iv.setOnClickListener(listener);
        }else {
            back_iv.setOnClickListener(view -> activity.finish());
        }
        return this;
    }

    public ToolbarUtil setTitle(String title) {
        TextView title_tv = (TextView) toolbar.getChildAt(1);
        title_tv.setText(title);
        return this;

    }

    public ToolbarUtil setTitleColor(int color) {
        TextView title_tv = (TextView) toolbar.getChildAt(1);
        title_tv.setTextColor(color);
        return this;

    }

    public ToolbarUtil setRightIcon(Integer right_img, View.OnClickListener listener) {
        TextView right_tv = (TextView) toolbar.getChildAt(2);
        ImageView right_iv = (ImageView) toolbar.getChildAt(3);
        if(right_img != null){
            right_tv.setVisibility(View.GONE);
            right_iv.setVisibility(View.VISIBLE);
            right_iv.setImageResource(right_img);
        }
        if(listener != null){
            right_iv.setOnClickListener(listener);
        }
        return this;
    }

    public ToolbarUtil setRightStr(String right_str, View.OnClickListener listener) {
        TextView right_tv = (TextView) toolbar.getChildAt(2);
        ImageView right_iv = (ImageView) toolbar.getChildAt(3);
        if(right_str != null){
            right_tv.setVisibility(View.VISIBLE);
            right_iv.setVisibility(View.GONE);
            right_tv.setText(right_str);
        }
        if(listener != null){
            right_tv.setOnClickListener(listener);
        }
        return this;
    }

    public ToolbarUtil setRightStrColor(int color) {
        TextView right_tv = (TextView) toolbar.getChildAt(2);
        right_tv.setTextColor(color);
        return this;

    }

    public void build(){
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

}
